﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Collections.Generic;
using HRProject.Code.DTO;
using HRProject.Code.DAO;

namespace HRProject.User
{
    public partial class ManageUser : System.Web.UI.Page
    {
        private List<BLAudit> allAudits;
        private List<UserObject> allUsers;

        public List<UserObject> AllUsers
        {
            get { return allUsers; }
        }

        public List<BLAudit> AllAudits
        {
            get { return allAudits; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack)
            {
                AuditDAO auditDAO = new AuditDAO();
                allAudits = auditDAO.GetAll();

                allUsers = new List<UserObject>();
                MembershipUserCollection users = Membership.GetAllUsers();
                foreach (MembershipUser u in users)
                {
                    BLUser user = new BLUser(u);
                    List<BLAudit> audits = auditDAO.Search((Guid)user.ProviderUserKey);
                    allUsers.Add(new UserObject(user, audits));
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            AuditDAO auditDao = new AuditDAO();
            List<UserObject> users = new List<UserObject>();
            int errorCode = 0;
            int updateResult;

            string[] sUserId = Request.Params.GetValues("hdfUserId");
            string[] sAudit = Request.Params.GetValues("hdfAuditType");
            string[] sMode = Request.Params.GetValues("hdfAuditMode");
            if (sUserId == null || sAudit == null || sMode == null
                || sUserId.Length != sAudit.Length || sUserId.Length != sMode.Length)
            {
                return;
            }

            try
            {
                Guid userId;
                for (int i = 0; i < sUserId.Length; i++)
                {
                    userId = new Guid(sUserId[i]);
                    updateResult = auditDao.UpdateAudit(userId, sAudit[i], sMode[i], ref errorCode);
                    if (errorCode == 12)
                    {
                        break;
                    }
                }
            }
            catch (Exception exp)
            {
                Console.Out.WriteLine(exp.Message);
            }

            Response.Redirect(Request.Url.ToString());
        }
    }
}
